`
https://leetcode.cn/problems/pancake-sorting/
`

/**
 * @param {number[]} arr
 * @return {number[]}
 */
var pancakeSort = function (cakes) {
  // 记录反转操作序列
  const res = [];

  const sort = function (n) {
    // base case
    if (n == 1) return;

    // 寻找最大饼的索引
    let maxCake = 0;
    let maxCakeIndex = 0;
    for (let i = 0; i < n; i++)
      if (cakes[i] > maxCake) {
        maxCakeIndex = i;
        maxCake = cakes[i];
      }

    // 第一次翻转，将最大饼翻到最上面
    reverse(cakes, 0, maxCakeIndex);
    res.push(maxCakeIndex + 1);
    // 第二次翻转，将最大饼翻到最下面
    reverse(cakes, 0, n - 1);
    res.push(n);

    // 递归调用
    sort(n - 1);
  };

  sort(cakes.length);
  return res;
};

function reverse(arr, i, j) {
  while (i < j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    i++; j--;
  }
};